package Dungeoneering.Puzzles;

import java.util.Iterator;

import org.powerbot.game.api.wrappers.node.SceneObject;


import Dungeoneering.Entities.Room;
import Dungeoneering.Utils.Utilities;
import Dungeoneering.api.methods.Objects;

public class CrystalPuzzle extends PuzzleStrategy{

	private static final int UNP_BLUE = 49472;
	private static final int UNP_YELLOW = 49499;
	private static final int UNP_GREEN = 49490;
	private static final int UNP_RED = 49481;

	private static final int P_BLUE = 49475;
	private static final int P_YELLOW = 49502;
	private static final int P_GREEN = 49493;
	private static final int P_RED = 49484;
	
	private static final int PAD = 52206;
	private static final int TILE_UNLIT = 49466;
	
	private static final int TILE_BLUE = 49469;
	private static final int TILE_YELLOW = 49496;
	private static final int TILE_GREEN = 49487;
	private static final int TILE_RED = 49478;
	
	private static final int BULB_UNLIT = 49508;
	private static final int BULB_LIT = 49511;
	private static final int BULB_COMPLETE = 49514;
	
	@Override
	public boolean detect(Room room) {
		if(room.containsOneOfLocations(BULB_UNLIT, BULB_LIT))
			return true;	
		return false;
	}

	@Override
	public void solve(Room room) {
		// TODO solve it :p
		boolean blue = false;
		boolean green = false;
		boolean red = false;
		boolean yellow = false;
		
		boolean timeBlue = false;
		boolean timeGreen = false;
		boolean timeYellow = false;
		boolean timeRed = false;
		
		if(!detect(room))
			//TODO write an error message
			return;
		
		blue = room.containsLocations(P_BLUE);
		green = room.containsLocations(P_GREEN);
		red = room.containsLocations(P_RED);
		yellow = room.containsLocations(P_YELLOW);			
		
		if(!(blue || green || red || yellow)) {
			Utilities.thinkSleep();
			//TODO click blue crystal
		}
		
		if(!blue) {
			Utilities.thinkSleep();
			//TODO Walk next to blue crystal
			while(!blue)
				blue = powerCrystal(UNP_BLUE, P_BLUE, room);
		}
		if(!yellow) {
			Utilities.thinkSleep();
			//TODO Walk next to yellow crystal
			while(!yellow)
				blue = powerCrystal(UNP_YELLOW, P_YELLOW, room);
		}
		if(!green) {
			Utilities.thinkSleep();
			//TODO Walk next to green crystal
			while(!green)
				green = powerCrystal(UNP_GREEN, P_GREEN, room);
		}
		if(!red) {
			Utilities.thinkSleep();
			//TODO Walk next to red crystal
			while(!red)
				red = powerCrystal(UNP_RED, P_RED, room);
		}
		
		if (room.containsLocations(BULB_COMPLETE))
			return;
		
		if(inTime(TILE_BLUE, TILE_RED, room)) {
			timeRed = timeBlue = true;
			if(!(timeGreen = inTime(TILE_BLUE, TILE_GREEN, room)))
				timeYellow = inTime(TILE_BLUE, TILE_YELLOW, room);
		}
		else if(inTime(TILE_BLUE, TILE_GREEN, room)) {
			timeGreen = timeBlue = true;
			if(!(timeRed = inTime(TILE_BLUE, TILE_RED, room)))
				timeYellow = inTime(TILE_BLUE, TILE_YELLOW, room);
		}
		else if(inTime(TILE_BLUE, TILE_YELLOW, room)) {
			timeYellow = timeBlue = true;
			if(!(timeGreen = inTime(TILE_BLUE, TILE_GREEN, room)))
				timeRed = inTime(TILE_BLUE, TILE_RED, room);
		}
		else if(inTime(TILE_YELLOW, TILE_GREEN, room)) {
			timeYellow = timeGreen = true;
			if(!(timeRed = inTime(TILE_YELLOW, TILE_RED, room)))
				timeBlue = inTime(TILE_YELLOW, TILE_BLUE, room);
		}
		else if(inTime(TILE_YELLOW, TILE_RED, room)) {
			timeYellow = timeRed = true;
			if(!(timeBlue = inTime(TILE_YELLOW, TILE_RED, room)))
				timeGreen = inTime(TILE_YELLOW, TILE_GREEN, room);
		}
		else if(inTime(TILE_RED, TILE_GREEN, room)) {
			timeRed = timeGreen = true;
			if(!(timeBlue = inTime(TILE_RED, TILE_BLUE, room)))
				timeYellow = inTime(TILE_RED, TILE_YELLOW, room);
		}

		while(!(blue && green && red && yellow))
			;//TODO stand on pads till in time
	}
	
	private boolean powerCrystal(int unpId, int pId, Room room) {
			if(room.containsLocations(BULB_LIT)) {
				Utilities.thinkSleep();
				//TODO click unpId
			}
			return room.containsLocations(pId);
	}
	private boolean inTime(int tileId1, int tileId2, Room room) {
		double distance1 = -1;
		double distance2 = -1;
		
		while (distance1 < 0 && distance2 < 0) {
			Utilities.sleep(10);
			Iterator<SceneObject[]> it = room.getLocations().values().iterator();
			while (it.hasNext()) {
				for(SceneObject findTile : it.next()) {
					if(findTile.getId() == tileId1)
						distance1 = findTile.getCentralPoint().distance(Objects.getNearest(PAD).getCentralPoint());
					else if (findTile.getId() == tileId2)
						distance2 = findTile.getCentralPoint().distance(Objects.getNearest(PAD).getCentralPoint());
				}
			}
		}
		
		if(distance1 == distance2)
			return true;
		else
			return false;
	}

	private boolean standPad(int coloredTile, Room room) {
		return false;
	}
	
	
}
